<template>
  <el-drawer
    v-model="drawerVisible"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    size="1000px"
    :title="drawerProps.title"
  >
    <div class="step flx" v-for="(step, sindex) in stepList" :key="sindex">
      <div class="index-line">
        <div class="index bg-primary">{{ step.index }}</div>
        <div class="line"></div>
      </div>
      <div class="step-content flx-1" :class="{ 'put-away': step.launched }">
        <div class="header" @click="step.launched = !step.launched">
          <div class="title flx-justify-between">
            <div class="title-text text-primary">{{ step.title }}</div>
            <div class="text-primary" v-if="!step.launched">
              收起<el-icon><CaretTop /></el-icon>
            </div>
            <div class="text-grey" v-if="step.launched">
              展开<el-icon><CaretBottom /></el-icon>
            </div>
          </div>
          <div class="desc">{{ step.desc }}</div>
        </div>

        <div class="step-main">
          <!-- 合同信息 -->
          <template v-if="sindex === 0">
            <el-form
              ref="ruleFormRef"
              label-position="top"
              require-asterisk-position="right"
              :inline="true"
              :rules="rules"
              :disabled="drawerProps.isView"
              :model="drawerProps.params"
              class="form-inline-wrap"
            >
              <el-form-item label="合同编号">
                <el-input v-model="drawerProps.params!.number" placeholder="请输入合同编号" clearable></el-input>
              </el-form-item>
              <el-form-item label="合同名称" prop="contractName">
                <el-input v-model="drawerProps.params!.contractName" placeholder="请输入合同名称" clearable></el-input>
              </el-form-item>
              <el-form-item label="客户名称" prop="customerName">
                <el-input v-model="drawerProps.params!.customerName" placeholder="请输入客户名称" clearable></el-input>
              </el-form-item>
              <el-form-item label="合同模板" prop="contractTemplate">
                <div class="flx w-100">
                  <el-select
                    class="flx-1 mr-10"
                    v-model="drawerProps.params!.contractTemplate"
                    placeholder="请选择合同模板"
                    clearable
                  >
                    <el-option v-for="item in contractTemplate" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                  <el-button plain type="primary" @click="() => {}">预览</el-button>
                  <el-button plain type="primary" @click="() => {}">上传</el-button>
                </div>
              </el-form-item>
              <el-form-item label="签署甲方" prop="signPartyA">
                <el-input v-model="drawerProps.params!.signPartyA" placeholder="请输入签署甲方" clearable></el-input>
              </el-form-item>
              <el-form-item label="签署乙方" prop="signPartyB">
                <el-input v-model="drawerProps.params!.signPartyB" placeholder="请输入签署乙方" clearable></el-input>
              </el-form-item>
              <el-form-item label="收款账户" prop="collectionAccount">
                <el-select v-model="drawerProps.params!.collectionAccount" placeholder="请选择收款账户" clearable>
                  <el-option v-for="item in contractTemplate" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
              <el-form-item label="签署日期">
                <el-date-picker
                  v-model="drawerProps.params!.dateOfSignature"
                  type="date"
                  placeholder="请选择签署日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                ></el-date-picker>
              </el-form-item>
              <el-form-item label="签署有效期">
                <el-date-picker
                  v-model="drawerProps.params!.validityPeriod"
                  type="datetimerange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  ref="timePick"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                >
                </el-date-picker>
              </el-form-item>
            </el-form>
          </template>

          <!-- 业务信息 -->
          <BusinessInfo v-if="sindex === 1" />

          <!-- 收款信息 -->
          <collection-info v-if="sindex === 2"></collection-info>
        </div>
      </div>
    </div>

    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" plain>暂存</el-button>
      <el-button type="primary" plain>预览合同</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">生成合同</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import BusinessInfo from "./BusinessInfo.vue";
import CollectionInfo from "./CollectionInfo.vue";

// 步骤
const stepList = ref([
  { index: 1, title: "合同信息", desc: "填写合同基础信息" },
  { index: 2, title: "业务信息", desc: "填写合同业务信息" },
  { index: 3, title: "收款信息", desc: "填写合同收款信息" }
]);

const rules = reactive({
  username: [{ required: true, message: "请输入客户姓名" }],
  contact: [{ required: true, message: "请输入联系方式" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  params: object;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  params: {}
});

// 合同模板
const contractTemplate = ref([
  { label: "111", value: 1 },
  { label: "222", value: 2 }
]);

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      //   await drawerProps.value.api!(drawerProps.value.row);
      //   ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
      //   drawerProps.value.search!();
      //   drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>

<style lang="scss" scoped>
.step {
  .index-line {
    position: relative;

    .index {
      width: 24px;
      height: 24px;
      line-height: 24px;
      border-radius: 50%;
      color: #fff;
      text-align: center;
      position: relative;
      z-index: 999;
    }
    .line {
      position: absolute;
      left: 12px;
      top: 0;
      width: 1px;
      height: 100%;
      background: #a8abb2;
    }
  }

  .step-content {
    padding-left: 12px;

    &.put-away {
      height: 66px;
      overflow: hidden;

      .step-main {
        height: 0;
        opacity: 0;
      }
    }
    .header {
      margin-bottom: 10px;
      cursor: pointer;

      .title-text {
        line-height: 22px;
      }
      .desc {
        font-size: 12px;
        line-height: 20px;
        color: #606266;
      }
    }

    .step-main {
      opacity: 1;
      transition: all 0.3s ease-in-out;
    }
  }

  &:last-child {
    .index-line,
    .line {
      background: #fff;
    }
  }
}
</style>
